
//
//  VEAssociationIntrodViewController.m
//  VEMoneyPorject
//
//  Created by BlueWind on 2018/9/13.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "VEAssociationIntrodViewController.h"
#import "HFStretchableTableHeaderView.h"
#import "VEAssionIntrodHeadTableViewCell.h"

#import "VEAssionIntrodSummaryTableViewCell.h"
#import "VEAssionTitleTableViewCell.h"
#import "VEAssionIntrodSummaryTableViewCell.h"
#import "VELoginOutTableViewCell.h"
#import "VEAssionUserTableViewCell.h"
#import "VEAssionModel.h"


@interface VEAssociationIntrodViewController ()
@property (nonatomic, strong) HFStretchableTableHeaderView* stretchableTableHeaderView;

@end

@implementation VEAssociationIntrodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.backgroundColor = [UIColor colorWithHexString:kCommonBgColor];
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, -20, 0);
    // 可以刷新可以加载更多
    self.refreshType = CJBaseTableVcRefreshTypeOnlyCanRefresh;
    [self cj_refresh];
}


- (void)cj_refresh{
    [super cj_refresh];
    [self loadData];
}

- (void)loadData{
    [super loadData];
    [self requestDataWithType:1];
    
}


- (void)requestDataWithType:(NSInteger)type{
    
    NSString *url = [NSString stringWithFormat:@"%@/VEService/GetSQJieshao.do",kPublicInterfaceAPI];
    CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:url isPost:YES withParameters:@{@"shequnid":self.assionId}];
    [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
        self.isSuccessNet = YES;
        [self cj_endRefresh];
        [self cj_endLoadMore];
        if (![NSObject isNilOrNull:response]) {
            NSArray *arr = response;
            if (arr.count == 0) {
                self.page -=1;
                return;
            }
            
            self.dataArray = [VEAssionModel mj_objectArrayWithKeyValuesArray:arr];

            [self.tableView reloadData];
        }
    }];
}
- (NSInteger)cj_numberOfSections {
    return 3;
}

/**
 某个cell的数量
 */
- (NSInteger)cj_numberOfRowsInsection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 2;
    } else {
        return 3;
    }
}

- (CGFloat)cj_cellHeightAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    NSString *str = @"";
    
    if (self.dataArray.count > 0) {
        VEAssionModel *model = self.dataArray[0];
        
        str = model.COMMUNITYPRESENT;
    }
    
    if (indexPath.section == 0) {
        return 330;
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return 60;
        } else {
            CGFloat h = [str heigthWithWeight:(SCREEN_WIDTH - 30) andFont:16 andLineSpacing:4 andWordSpacing:2];
            return h + 38;
        }
    } else {
        if (indexPath.row == 0) {
            return 60;

        } else if (indexPath.row == 1) {
            return 180;
        } else {
            return 90;

        }
    }

    
}

/**
 某行的cell
 */
- (CJBaseTableViewCell *)cj_cellAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        VEAssionIntrodHeadTableViewCell *cell = [VEAssionIntrodHeadTableViewCell cellWithTableView:self.tableView];
        if (self.dataArray.count > 0) {
            cell.model = self.dataArray[0];
        }
        return cell;
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            VEAssionTitleTableViewCell *cell = [VEAssionTitleTableViewCell cellWithTableView:self.tableView];
            return cell;
        } else {
            VEAssionIntrodSummaryTableViewCell *cell = [VEAssionIntrodSummaryTableViewCell cellWithTableView:self.tableView];
            if (self.dataArray.count > 0) {
                cell.model = self.dataArray[0];
            }
            return cell;
        }
    } else {
        if (indexPath.row == 0) {
            VEAssionTitleTableViewCell *cell = [VEAssionTitleTableViewCell cellWithTableView:self.tableView];
            return cell;
        } else if (indexPath.row == 1) {
            VEAssionUserTableViewCell *cell = [VEAssionUserTableViewCell cellWithTableView:self.tableView];
            return cell;
        } else {
            VELoginOutTableViewCell *cell = [VELoginOutTableViewCell cellWithTableView:self.tableView];
            cell.isJion = self.isJoin;
            cell.loginOutAssion = ^{
                if (self.dataArray.count > 0) {
                    VEAssionModel *model = self.dataArray[0];
                    NSString *userId = [[CJUserInfoManager sharedManager] currentUserInfo].USERID;
                    
                    if ([NSObject isNilOrNull:userId]) {
                        userId = @"";
                    }
                    
                    CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:[NSString stringWithFormat:@"%@/VEService/ExitShequn.do",kPublicInterfaceAPI] isPost:YES withParameters:@{@"shequnid":model.COMMUNITYID,@"Userid":userId}];
                    [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
                        
                        [CJUIKit showSuccessWithStatus:message];
                        
                        [self.navigationController popViewControllerAnimated:YES];
                        
                    }];
                }
            };
            return cell;
        }
    }

}

@end
